package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.InsuranceContent;
import cn.wolfcode.trip.base.domain.InsuranceProduct;
import cn.wolfcode.trip.base.domain.Strategy;
import cn.wolfcode.trip.base.mapper.InsuranceContentMapper;
import cn.wolfcode.trip.base.mapper.InsuranceProductMapper;
import cn.wolfcode.trip.base.query.InsuranceQuery;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.service.IInsuranceProductService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Service
public class InsuranceProductServiceImpl implements IInsuranceProductService {

    @Autowired
    InsuranceProductMapper insuranceProductMapper;
    @Autowired
    InsuranceContentMapper insuranceContentMapper;


    public PageInfo query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize(), qo.getOrderBy());
        List<?> list = insuranceProductMapper.selectForList(qo);
        return new PageInfo<>(list);
    }

    @Override
    public void saveOrUpdate(InsuranceProduct entity) {
        // 1、获取保险产品内容
        InsuranceContent insuranceContent = entity.getInsuranceContent();
        Date date = new Date();

        // 2、保存产品信息（设计两张表）
        if (entity.getId() != null) {// 修改保存
            insuranceProductMapper.updateByPrimaryKey(entity);
            insuranceContent.setId(entity.getId());
            insuranceContentMapper.updateByPrimaryKey(insuranceContent);

        } else {// 新增保存
            // 将创建时间设置给新产品
            entity.setCreateTime(date);
            insuranceProductMapper.insert(entity);

            // insuranceContent表主键不自增长，id等于insuranceproduct表id
            Long id = entity.getId();
            insuranceContent.setId(id);
            insuranceContentMapper.insert(insuranceContent);
        }

    }

    @Override
    public PageInfo queryForApp(InsuranceQuery qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize(), qo.getOrderBy());
        List<HashMap> list = insuranceProductMapper.selectForAppList(qo);
        return new PageInfo<>(list);
    }

    @Override
    public PageInfo queryForShows(InsuranceQuery qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize(), qo.getOrderBy());
        List<?> list = insuranceProductMapper.selectForPubProducts(qo);
        return new PageInfo<>(list);
    }

    @Override
    public InsuranceProduct getProductById(Long id) {
        InsuranceProduct insuranceProduct = insuranceProductMapper.getInsuranceProductById(id);
        return insuranceProduct;
    }

    @Override
    public void deleteById(Long id) {
        // 删除指定到的保险产品，先删除该产品在内容表中对应的记录，再删除产品表中的记录
        // 1、insurancecontent内容表
        insuranceContentMapper.deleteByPrimaryKey(id);

        // 2、insuranceproduct产品表
        insuranceProductMapper.deleteByPrimaryKey(id);
    }


}
